run programs
set more off

cap log close
log using graphs.log, replace

local base1="alleducM20"
local baselab1="(95% confidence intervals for male, age 20)"

local base2="alleducF20"
local baselab2="(95% confidence intervals for female, age 20)"

local base3="alleducF40"
local baselab3="(95% confidence intervals for female, age 40)"

local base4="alleducM40"
local baselab4="(95% confidence intervals for male, age 40)"


local base5="alleducF60"
local baselab5="(95% confidence intervals for female, age 60)"

local base6="alleducM60"
local baselab6="(95% confidence intervals for male, age 60)"



local vars1 "hetero  nop seat pool ballot lnw `means'"
global varhetero "hetero  med_nop med_seat med_pool med_ballot med_lnw `medmeans'"
global varnop "med_hetero  nop med_seat med_pool med_ballot med_lnw `medmeans'"
global varseat "med_hetero  med_nop seat med_pool med_ballot med_lnw `medmeans'"
global varpool "med_hetero  med_nop med_seat pool med_ballot med_lnw `medmeans'"
global varballot "med_hetero  med_nop med_seat med_pool ballot med_lnw `medmeans'"
global varlnw "med_hetero  med_nop med_seat med_pool med_ballot lnw `medmeans'"


forval i=1/6 {
local base="`base`i''"
local baselab="`baselab`i''"


local grfmt=".tif"
*local orient="orientation(landscape)  tm(0) lm(0)"

use `base', clear
/* rvars is a program in programs.do */
rvars

label var A2001 "Mean Age"
label var A2003 "Mean Education"  
label var nop "Number of Electoral Parties"
label var seat "Eff. Number of Leg. Parties"
label var pool "Vote for Candidate"
label var hetero "Social Heterogeneity"
label var lnw "Log Weighted Party Age"

  
dropc 1

local level1="Education"
global maxlevel1=8
global skip=1
global w=1

**recenter macrolevel variables at their medians
foreach var of varlist `vars1' {
qui  sum `var',detail
qui  gen med_`var'=`var'-r(p50)
}



**************************
qui {
  setgraph seat
  
  if `i'==3 {
    setgraph ballot
    setgraph pool
    setgraph lnw
    setgraph hetero
    setgraph nop
  }
}
**************************


foreach var of varlist min* max* mean* {
  replace `var'=norm(`var')
}

tempfile test
save `test', replace
*set trace on
  

use `test', clear
gen x=[_n] in 1/$maxlevel1
label var x `"`level1'"'
local subtitle=`"subtitle(`"`baselab'"')"'
global groptions `"ylabel(0(.2)1) ytitle(`"Probability of Party Attachment"') scale(.6) xsize(4) ysize(3) `subtitle'  scheme(s1mono)"' 




if `i'==4 {
  dograph seat "graph8" "Figure 8. Legislative fractionalization and party attachments" "across education levels" 
graph export figure8`grfmt', replace
}



if `i'==5 {
  dograph seat "graph9" "Figure 9. Legislative fractionalization and party attachments" "across education levels"
graph export figure9`grfmt', replace
}

if `i'==6 {
  dograph seat "graph7" "Figure 7. Legislative fractionalization and party attachments" "across education levels"
graph export figure7`grfmt', replace
}

if `i'==2 {
  dograph seat "graph10" "Figure 10. Legislative fractionalization and party attachments" "across education levels"
graph export figure10`grfmt', replace
}



if `i'==3 {
dograph nop graph4 "Figure 4. Electoral choice and party attachments" "across education levels" 
graph export figure4`grfmt', replace

dograph hetero graph5 "Figure 5. Social heterogeneity and party attachments" "across education levels"
graph export figure5`grfmt', replace

dograph lnw graph6 "Figure 6. Party age and party attachments" "across education levels"
graph export figure6`grfmt', replace

dograph pool "graph3" "Figure 3. Ballot Structure and party attachments" "across education levels"
graph export figure3`grfmt', replace

dograph seat "graph2" "Figure 2. Legislative fractionalization and party attachments" "across education levels"
graph export figure2`grfmt', replace
}


if `i'==1 {
  dograph seat "graph11" "Figure 11. Legislative fractionalization and party attachments"  "across education levels"
graph export figure11`grfmt', replace
}

}
